import java.awt.Color;
import java.awt.Dimension;
import java.awt.Paint;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.JFrame;
import org.apache.commons.collections15.Transformer;

import edu.uci.ics.jung.algorithms.layout.ISOMLayout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;


@SuppressWarnings("serial")
public class AutomataVisualizer implements Serializable{
	 private DirectedSparseGraph<String, String> g;
	 private Integer numVertex;
	 private Integer numEdge;
	 private VisualizationViewer<String, String> vv;
	 private Transformer<String, String> transformer;
	 private Transformer<String, Paint> transformerColor;
	 public static Vector<String> final_vertex;
	 Map<String,String> mpEdge;
	 
	public AutomataVisualizer() {
		 mpEdge=new HashMap<String, String>();
		 g = new DirectedSparseGraph<String, String>();
		 numVertex = 0;
		 numEdge=0;
		 final_vertex = new Vector<String>();
		 
	}
	
	public void view(int larg, int alt) {
		vv = new VisualizationViewer<String, String>(
				new ISOMLayout<String, String>(g), new Dimension(larg, alt));
		transformer = new Transformer<String, String>() {
			@Override
			public String transform(String arg0) {
				return arg0.toString();
			}
		};
		vv.getRenderContext().setVertexLabelTransformer(transformer);
		transformerColor = new Transformer<String,Paint>() {
            public Paint transform(String i) {
            	if(final_vertex.contains(i))
            		  return (Paint) Color.RED;
                return (Paint) Color.GREEN;
            }
        };  
        vv.getRenderContext().setVertexFillPaintTransformer(transformerColor);
		transformer = new Transformer<String, String>() {
			@Override
			public String transform(String arg0) {
				return mpEdge.get(arg0);
			}
		};
		vv.getRenderContext().setEdgeLabelTransformer(transformer);
		//vv.getRenderer().setVertexRenderer(new MyRenderer());

		// The following code adds capability for mouse picking of
		// vertices/edges. Vertices can even be moved!
		final DefaultModalGraphMouse<String, Number> graphMouse = new DefaultModalGraphMouse<String, Number>();
		vv.setGraphMouse(graphMouse);
		graphMouse.setMode(ModalGraphMouse.Mode.PICKING);

		JFrame frame = new JFrame();
		frame.getContentPane().add(vv);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public int addVertex(){
		
		g.addVertex(numVertex.toString());
		numVertex++;
		return numVertex - 1;
	}
	
	public void addEdge(String name,Integer source, Integer dest){
		
		g.addEdge(numEdge.toString(), source.toString(), dest.toString());
		mpEdge.put(numEdge.toString(), name);
		numEdge++;
		
		
	}
	
	
	  
	   
	
}
